﻿using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;



namespace DocuChart.PieChartEditor
{

    [AspNetHostingPermission(SecurityAction.Demand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    
    public class PieChartWebpartEditor : EditorPart
    {
        private Label lblDebug;
        private Label lblLists;
        private DropDownList ddlLists;
        private Label lblChartType;
        private DropDownList ddlChartType;
        private CheckBox chkbx3Denabled;
        private CheckBox chkbxHighlight;
        private CheckBox chkbxTotal;
        private CheckBox chkbxLegend;
        private Label lblPalette;
        private DropDownList ddlPalette;
        
        private string[] PaletteArray = new string[] { "Bright", "Grayscale", "Excel", "Light", "Pastel", "EarthTones", "SemiTransparent", "Berry", "Chocolate", "Fire", "SeaGreen", "BrightPastel" };
        // NEED TO ADD PANEL CONTROLS, EVENT HANDLER FOR ddlChartType, and logic for PieChart labels and legend.
        
        

        
        
        
        protected override void CreateChildControls()
        {
            lblLists = new Label();
            lblLists.Text = "Please select the list";
            Controls.Add(lblLists);

            ddlLists = new DropDownList();
            Controls.Add(ddlLists);

            lblChartType = new Label();
            lblChartType.Text = "Please select the chart type";
            Controls.Add(lblChartType);
            
            ddlChartType = new DropDownList();
            ddlChartType.Items.Add("Pie");
            ddlChartType.Items.Add("Bar");
            ddlChartType.SelectedIndexChanged += new EventHandler(ddlChartType_SelectedIndexChanged);
            ddlChartType.AutoPostBack = true;
            Controls.Add(ddlChartType);

            chkbx3Denabled = new CheckBox();
            chkbx3Denabled.Text = "3D Enabled?";
            chkbx3Denabled.Checked = false;
            Controls.Add(chkbx3Denabled);

            chkbxHighlight = new CheckBox();
            chkbxHighlight.Text = "Highlight";
            chkbxHighlight.Checked = false;
            Controls.Add(chkbxHighlight);

            chkbxTotal = new CheckBox();
            chkbxTotal.Text = "Totals";
            chkbxTotal.Checked = false;
            Controls.Add(chkbxTotal);

            chkbxLegend = new CheckBox();
            chkbxLegend.Text = "Legend";
            chkbxLegend.Checked = false;
            Controls.Add(chkbxLegend);

            lblPalette = new Label();
            lblPalette.Text = "Please select a color palette";
            Controls.Add(lblPalette);
            
            ddlPalette = new DropDownList();
            foreach (string Palette in PaletteArray)
            {
                ddlPalette.Items.Add(Palette);
            }
            Controls.Add(ddlPalette);

            lblDebug = new Label();

                        
            SPSite oSiteCollection = SPContext.Current.Site;
            SPWebCollection collWebsite = oSiteCollection.AllWebs;

            for (int i = 0; i < collWebsite.Count; i++)
            {
                using (SPWeb oWebsite = collWebsite[i])
                {
                    SPListCollection collList = oWebsite.Lists;
                    
                    Guid DocumentLibraryFeature = new Guid("00BFEA71-E717-4E80-AA17-D0C71B360101");
                    
                    for (int j = 0; j < collList.Count; j++)
                    {
                        if (collList[j] is SPDocumentLibrary && collList[j].TemplateFeatureId.Equals(DocumentLibraryFeature))
                        {
                            ddlLists.Items.Add(collList[j].Title);
                        }
                    }
                }
            }
                      
            


            

          
            
        }

        void ddlChartType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlChartType.SelectedValue == "Pie")
            {
                chkbxLegend.Visible = true;
                chkbxLegend.Enabled = true;
            }
            else
            {
                
                chkbxLegend.Visible = false;
           }
        }

        // Save settings to web part
        public override bool ApplyChanges()
        {
            EnsureChildControls();
            PieChart.PieChart part =
                   (PieChart.PieChart)WebPartToEdit;
            part._SelectedList = ddlLists.SelectedValue;
            part._ChartType = ddlChartType.SelectedValue;
            part._3d = chkbx3Denabled.Checked;
            part._Highlight = chkbxHighlight.Checked;
            part._Totals = chkbxTotal.Checked;
            part._Legend = chkbxLegend.Checked;
            part._Palette = ddlPalette.SelectedValue;
            
            return true;
        }

        // Retrieve settings from web part
        public override void SyncChanges()
        {
            EnsureChildControls();
            PieChart.PieChart part =
                    (PieChart.PieChart)WebPartToEdit;

            if (part._SelectedList != null || part._ChartType != null || part._Palette != null) {

                
                
                ddlLists.SelectedValue = part._SelectedList;
                ddlChartType.SelectedValue = part._ChartType;
                chkbx3Denabled.Checked = part._3d;
                chkbxHighlight.Checked = part._Highlight;
                chkbxTotal.Checked = part._Totals;
                ddlPalette.SelectedValue = part._Palette;

               if (part._ChartType == "Pie")
            {
                chkbxLegend.Visible = true;
                chkbxLegend.Enabled = true;
                chkbxLegend.Checked = part._Legend;
            }
            else
            {
                chkbxLegend.Visible = false;
            }
        }
        return;
            }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            lblLists.RenderControl(writer);
            writer.WriteBreak();
            ddlLists.RenderControl(writer);
            writer.WriteBreak();
            writer.WriteBreak();
            lblLists.RenderControl(writer);
            writer.WriteBreak();
            ddlChartType.RenderControl(writer);
            writer.WriteBreak();
            writer.WriteBreak();
            chkbx3Denabled.RenderControl(writer);
            writer.WriteBreak();
            writer.WriteBreak();
            chkbxHighlight.RenderControl(writer);
            writer.WriteBreak();
            writer.WriteBreak();
            chkbxTotal.RenderControl(writer);
            writer.WriteBreak();
            writer.WriteBreak();
            chkbxLegend.RenderControl(writer);
            writer.WriteBreak();
            lblPalette.RenderControl(writer);
            writer.WriteBreak();
            ddlPalette.RenderControl(writer);
            writer.WriteBreak();
            writer.WriteBreak();
            

        }


        

        
    }
}
